package svntest

import org.tmatesoft.svn.core.SVNCommitInfo
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDeltaGenerator;
import org.tmatesoft.svn.core.wc.SVNWCUtil

import com.sun.corba.se.spi.activation.Repository;

class SvnTestController {

    def index() {
		try {
			DAVRepositoryFactory.setup()
			String url = 'https://yrn-test-web-proj.googlecode.com/svn/trunk/'
			String fileUrl = 'SvnTestController.groovy'
			
			SVNRepository repo = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url))
			ISVNAuthenticationManager manager = SVNWCUtil.createDefaultAuthenticationManager('yuri.ruchin@gmail.com', 'sq8aP6Tz5Nt6')
			repo.setAuthenticationManager(manager)
			boolean fileExists = (repo.checkPath(fileUrl, -1) == SVNNodeKind.FILE)
			
			
			ISVNEditor editor = repo.getCommitEditor('Content commit test', null)
	
			editor.openRoot(-1)
			
			editor.addDir('testDir1/testDir2', null, -1)
			editor.closeDir()
						
			if (!fileExists) {
				println 'file does not exist'
				editor.addFile(fileUrl, null, -1)
			} else {
				println 'file exists'
				editor.openFile(fileUrl, -1)
			}

			editor.applyTextDelta(fileUrl, null)
			SVNDeltaGenerator deltaGen = new SVNDeltaGenerator()
			
			String filepath = 'D:/homework/spring/workspace/SvnTest/grails-app/controllers/svntest/SvnTestController.groovy'
			
			String checksum = deltaGen.sendDelta(fileUrl, new BufferedInputStream(new FileInputStream(filepath)), editor, true)

			editor.closeFile(fileUrl, checksum)
			editor.closeDir()
			
			SVNCommitInfo commitInfo = editor.closeEdit()
			
			println commitInfo
		} catch (SVNException e) {
			e.printStackTrace()
		}
    }
}
